package com.lyy.weather.TIme;

import android.util.Log;

import com.lyy.weather.database.DatabaseUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

//关于时间的工具类
public class TimeUtil {
    //定义时间格式
    private static final String TIME_FORMAT="yyyy-MM-dd'T'HH:mm'+08:00'";
    private static final String TIME_FORMAT_HOURLY="HH:mm";
    private static final String TIME_FORMAT_DATE="MM月dd日";
    //获取当前系统时间的Date类对象
    public static Date getDate(){

        Calendar c=Calendar.getInstance();
       // Log.d("TAG", "getDate: "+c.getTimeZone().getID());
        return c.getTime();
    }

    //根据格式获取Date对象
    public static Date getDateInFormat(String time) throws ParseException {

        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(TIME_FORMAT);
        return simpleDateFormat.parse(time);
    }

    //根据格式获取时间的字符串
    //用于数据库交流
    public static String getDateString(Date date){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(TIME_FORMAT);
        return simpleDateFormat.format(date);
    }

    //根据格式获取时间的字符串
    //用于显示在控件上
    public static String getHourlyString(Date date){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(TIME_FORMAT_HOURLY);
        return simpleDateFormat.format(date);
    }

    //根据格式获取时间的字符串
    //用于显示在控件上
    public static String getDatelyString(Date date){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(TIME_FORMAT_DATE);
        return simpleDateFormat.format(date);
    }

    //Date对象转换为Calendar对象
    public Calendar dateToCalendar(Date date){
        Calendar c=Calendar.getInstance();

        c.setTime(date);
        return c;
    }

    //对比日期是否改变了或超过了八个小时
    //判断数据是否要进行更新
    public static boolean shouldItRefreshAll(Calendar old, Calendar current){
        if(old==null)
        {
            return true;
        }
        if(old.get(Calendar.YEAR)==current.get(Calendar.YEAR)){
            if(old.get(Calendar.MONTH)==current.get(Calendar.MONTH))
            {
                if(old.get(Calendar.DATE)==current.get(Calendar.DATE))
                {
                    long oldTime=old.getTime().getTime();
                    long currentTime=current.getTime().getTime();
                    int eightHour=8*60*60*1000;
                    if(currentTime-oldTime<eightHour)
                        return false;
                }
            }
        }

        return true;
    }

    //对比hourly中的第一个的时间和系统时间的HOUR，如果不一样，更新
    public static boolean shouldItRefresh24H(Calendar old,Calendar current){
        if(old==null)
        {
            return true;
        }
        if(old.HOUR_OF_DAY==current.HOUR_OF_DAY){
            return false;
        }
        return true;
    }



}
